#include "ctrlMouse.h"
#include <windows.h>
#include <QDebug>
#include "defines.h"

ctrlMouse::ctrlMouse(QObject *parent) :
    QObject(parent)
{
}

void ctrlMouse::activeLeftButton()
{
    if (s == MOUSE_DOWN)
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    }
    else if (s == MOUSE_MOVE)
    {
        mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);
    }
    else
    {
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
}

void ctrlMouse::activeRightButton()
{
    if (s == MOUSE_DOWN)
    {
        mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
    }
    else if (s == MOUSE_UP)
    {
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
    }
}

void ctrlMouse::activeMidButton()
{
}

void ctrlMouse::slotCtrlMouse(QByteArray byteArray)
{
    /*
      0(1)         1(1)          2(1)      3(1)     4(2)     6(1)   7(2)    9(2)     11(len-6)
      Flag(0xfe)  data len(1)  checksum  Main Cmd   Sub Cmd  state    x      y     other data
      */
    s = byteArray[6]&0xff;
    x = ((byteArray[7] & 0xff) << 8 | (byteArray[8] & 0xff));
    y = ((byteArray[9] & 0xff) << 8 | (byteArray[10] & 0xff));

    //if the data is negative, convert it.
    if (x>>15)
    {
        x = -((~(x-1))&0xffff);
    }
    if (y>>15)
    {
        y = -((~(y-1))&0xffff);
    }

    switch(byteArray[5] & 0xff)
    {
    case SCMD_LEFT_BUTTON:
        activeLeftButton();
        break;

    case SCMD_RIGHT_BUTTON:
        activeRightButton();
        break;

    case SCMD_MID_BUTTON:
        activeMidButton();
        break;
    }
}
